import asyncio
import playwright.async_api
from playwright.async_api import async_playwright
from settings import TARGET_NUM
from settings import BJ_PREFIX
import pipeline as pl
import os
import time
import sys
from settings import ORIGIN_BIJIAN_NEW_VIDEO_LINK, ORIGIN_UP_HOME_PAGE_LINK


print(os.getcwd())

async def extract_author(content: playwright.async_api.Page):
    loc = await content.locator('.detail-all-panel .up-name').all_text_contents()
    number = len(loc)
    return number

async def extract_author_href(content: playwright.async_api.Page):
    q = await content.query_selector_all('.detail-all-panel .up-name')
    links = []

    for i in range(len(q)):
        link = await q[i].get_attribute('href')
        links.append('https:' + link)
        if i > TARGET_NUM:
            break
    return links

async def extract_video_href(content: playwright.async_api.Page):
    q = await content.query_selector_all('.detail-all-panel .video-name')
    links = []

    for i in range(len(q)):
        link = await q[i].get_attribute('href')
        links.append('https:' + link)
        if i > TARGET_NUM:
            break
    return links

async def fetch(context):
    url = BJ_PREFIX
    page = await context.new_page()
    try:
        # following: `document`, `stylesheet`, `image`, `media`, `font`, `script`, `texttrack`, `xhr`, `fetch`,
        # `eventsource`, `websocket`, `manifest`, `other`.
        await page.route("**/*", lambda route: route.abort()
        if route.request.resource_type == "image"
        else route.continue_())
        await page.goto(url, wait_until="domcontentloaded") # domcontentloaded networkidle
        await page.get_by_text('最新投稿').click()
        number = await extract_author(page)
        while number < TARGET_NUM:
            await page.keyboard.down('End')
            number = await extract_author(page)
            print(number)
        links = await extract_author_href(page)
        video_links = await extract_video_href(page)
        pl.save_links(ORIGIN_UP_HOME_PAGE_LINK, links)
        pl.save_links(ORIGIN_BIJIAN_NEW_VIDEO_LINK, video_links)

    except playwright.async_api.TimeoutError as e:
        print(e)
    else:
        print('over')
        await page.close()

async def bilibili() -> None:
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        context = await browser.new_context()
        await fetch(context=context)
        await context.close()
        await browser.close()

def main(argv):
    loop = asyncio.get_event_loop()
    loop.run_until_complete(bilibili())


if __name__ == "__main__":
    main(sys.argv)

